home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / hpux.c < prev    next >
C/C++ Source or Header  |  1991-10-13  |  8KB  |  355 lines

  1. /* @(#) $Header: hpux.c,v 1.21 91/10/11 18:56:17 deyke Exp $ */
  2.  
  3. #include <sys/types.h>
  4.  
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/rtprio.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <termio.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15.  
  16. #include "global.h"
  17. #include "iface.h"
  18. #include "timer.h"
  19. #include "files.h"
  20. #include "hardware.h"
  21. #include "login.h"
  22. #include "commands.h"
  23. #include "main.h"
  24. #include "hpux.h"
  25.  
  26. #define TIMEOUT 120
  27.  
  28. static int  chkread[2];
  29. static int  actread[2];
  30. static void (*readfnc[_NFILE]) __ARGS((void *));
  31. static void *readarg[_NFILE];
  32.  
  33. static int  chkwrite[2];
  34. static int  actwrite[2];
  35. static void (*writefnc[_NFILE]) __ARGS((void *));
  36. static void *writearg[_NFILE];
  37.  
  38. static int  chkexcp[2];
  39. static int  actexcp[2];
  40. static void (*excpfnc[_NFILE]) __ARGS((void *));
  41. static void *excparg[_NFILE];
  42.  
  43. static int  nfds = -1;
  44.  
  45. static int  local_kbd;
  46.  
  47. static struct termio curr_termio;
  48. static struct termio prev_termio;
  49.  
  50. static void check_files_changed __ARGS((void));
  51.  
  52. /*---------------------------------------------------------------------------*/
  53.  
  54. static void sigpipe_handler(sig, code, scp)
  55. int  sig;
  56. int  code;
  57. struct sigcontext *scp;
  58. {
  59.   scp->sc_syscall_action = SIG_RETURN;
  60. }
  61.  
  62. /*---------------------------------------------------------------------------*/
  63.  
  64. void ioinit()
  65. {
  66.  
  67.   int i;
  68.   struct sigvec vec;
  69.  
  70. #define fixdir(name, mode) \
  71.     { mkdir((name), (mode)); chmod((name), (mode)); }
  72.  
  73.   fixdir("/tcp", 0755);
  74.   fixdir("/tcp/sockets", 0755);
  75.   fixdir("/tcp/.sockets", 0700);
  76.   fixdir("/tcp/logs", 0700);
  77.  
  78.   if (local_kbd = isatty(0)) {
  79.     ioctl(0, TCGETA, &prev_termio);
  80.     curr_termio = prev_termio;
  81.     curr_termio.c_iflag = BRKINT | ICRNL | IXON | IXANY | IXOFF;
  82.     curr_termio.c_lflag = 0;
  83.     curr_termio.c_cc[VMIN] = 0;
  84.     curr_termio.c_cc[VTIME] = 0;
  85.     ioctl(0, TCSETA, &curr_termio);
  86.     printf("\033&s1A");   /* enable XmitFnctn */
  87.     on_read(0, (void (*)()) keyboard, (void *) 0);
  88.   } else {
  89.     for (i = 0; i < _NFILE; i++) close(i);
  90.     setpgrp();
  91.     fopen("/dev/null", "r+");
  92.     fopen("/dev/null", "r+");
  93.     fopen("/dev/null", "r+");
  94.     remote_kbd_initialize();
  95.   }
  96.   vec.sv_mask = vec.sv_flags = 0;
  97.   vec.sv_handler = sigpipe_handler;
  98.   sigvector(SIGPIPE, &vec, (struct sigvec *) 0);
  99.   vec.sv_handler = (void (*)()) abort;
  100.   sigvector(SIGALRM, &vec, (struct sigvec *) 0);
  101.   if (!Debug) alarm(TIMEOUT);
  102.   umask(022);
  103.   if (!Debug) rtprio(0, 127);
  104.   if (!getenv("HOME"))
  105.     putenv("HOME=/users/root");
  106.   if (!getenv("LOGNAME"))
  107.     putenv("LOGNAME=root");
  108.   if (!getenv("PATH"))
  109.     putenv("PATH=/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin");
  110.   if (!getenv("SHELL"))
  111.     putenv("SHELL=/bin/sh");
  112.   if (!getenv("TZ"))
  113.     putenv("TZ=MEZ-1MESZ");
  114.  
  115.   {
  116.     /* Init times */
  117.     struct timeval tv;
  118.     struct timezone tz;
  119.     gettimeofday(&tv, &tz);
  120.     Secclock = tv.tv_sec;
  121.     Msclock = 1000 * Secclock + tv.tv_usec / 1000;
  122.   }
  123.  
  124.   fixutmpfile();
  125. }
  126.  
  127. /*---------------------------------------------------------------------------*/
  128.  
  129. void iostop()
  130. {
  131.   register struct iface *ifp;
  132.  
  133.   if (local_kbd) {
  134.     ioctl(0, TCSETA, &prev_termio);
  135.     printf("\033&s0A");   /* disable XmitFnctn */
  136.   }
  137.   for (ifp = Ifaces; ifp; ifp = ifp->next)
  138.     if (ifp->stop) (*ifp->stop)(ifp);
  139. }
  140.  
  141. /*---------------------------------------------------------------------------*/
  142.  
  143. int  system(cmdline)
  144. const char *cmdline;
  145. {
  146.  
  147.   int  i, pid, status;
  148.   long  oldmask;
  149.  
  150.   if (!cmdline) return 1;
  151.   fflush(stdout);
  152.   switch (pid = fork()) {
  153.   case -1:
  154.     return (-1);
  155.   case 0:
  156.     for (i = 3; i < _NFILE; i++) close(i);
  157.     execl("/bin/sh", "sh", "-c", cmdline, (char *) 0);
  158.     exit(1);
  159.   default:
  160.     oldmask = sigsetmask(-1);
  161.     while (wait(&status) != pid) ;
  162.     sigsetmask(oldmask);
  163.     return status;
  164.   }
  165. }
  166.  
  167. /*---------------------------------------------------------------------------*/
  168.  
  169. int  _system(cmdline)
  170. char  *cmdline;
  171. {
  172.   return system(cmdline);
  173. }
  174.  
  175. /*---------------------------------------------------------------------------*/
  176.  
  177. int  doshell(argc, argv, p)
  178. int  argc;
  179. char  *argv[];
  180. void *p;
  181. {
  182.   char  buf[2048];
  183.  
  184.   *buf = '\0';
  185.   while (--argc > 0) {
  186.     if (*buf) strcat(buf, " ");
  187.     strcat(buf, *++argv);
  188.   }
  189.   return system(buf);
  190. }
  191.  
  192. /*---------------------------------------------------------------------------*/
  193.  
  194. #define setmask(mask, fd) ((mask)[(fd)>>5] |=  (1 << ((fd) & 31)))
  195. #define clrmask(mask, fd) ((mask)[(fd)>>5] &= ~(1 << ((fd) & 31)))
  196. #define maskset(mask, fd) ((mask)[(fd)>>5] &   (1 << ((fd) & 31)))
  197.  
  198. /*---------------------------------------------------------------------------*/
  199.  
  200. void on_read(fd, fnc, arg)
  201. int  fd;
  202. void (*fnc) __ARGS((void *));
  203. void *arg;
  204. {
  205.   readfnc[fd] = fnc;
  206.   readarg[fd] = arg;
  207.   setmask(chkread, fd);
  208.   clrmask(actread, fd);
  209.   nfds = -1;
  210. }
  211.  
  212. /*---------------------------------------------------------------------------*/
  213.  
  214. void off_read(fd)
  215. int  fd;
  216. {
  217.   readfnc[fd] = 0;
  218.   readarg[fd] = 0;
  219.   clrmask(chkread, fd);
  220.   clrmask(actread, fd);
  221.   nfds = -1;
  222. }
  223.  
  224. /*---------------------------------------------------------------------------*/
  225.  
  226. void on_write(fd, fnc, arg)
  227. int  fd;
  228. void (*fnc) __ARGS((void *));
  229. void *arg;
  230. {
  231.   writefnc[fd] = fnc;
  232.   writearg[fd] = arg;
  233.   setmask(chkwrite, fd);
  234.   clrmask(actwrite, fd);
  235.   nfds = -1;
  236. }
  237.  
  238. /*---------------------------------------------------------------------------*/
  239.  
  240. void off_write(fd)
  241. int  fd;
  242. {
  243.   writefnc[fd] = 0;
  244.   writearg[fd] = 0;
  245.   clrmask(chkwrite, fd);
  246.   clrmask(actwrite, fd);
  247.   nfds = -1;
  248. }
  249.  
  250. /*---------------------------------------------------------------------------*/
  251.  
  252. void on_excp(fd, fnc, arg)
  253. int  fd;
  254. void (*fnc) __ARGS((void *));
  255. void *arg;
  256. {
  257.   excpfnc[fd] = fnc;
  258.   excparg[fd] = arg;
  259.   setmask(chkexcp, fd);
  260.   clrmask(actexcp, fd);
  261.   nfds = -1;
  262. }
  263.  
  264. /*---------------------------------------------------------------------------*/
  265.  
  266. void off_excp(fd)
  267. int  fd;
  268. {
  269.   excpfnc[fd] = 0;
  270.   excparg[fd] = 0;
  271.   clrmask(chkexcp, fd);
  272.   clrmask(actexcp, fd);
  273.   nfds = -1;
  274. }
  275.  
  276. /*---------------------------------------------------------------------------*/
  277.  
  278. static void check_files_changed()
  279. {
  280.  
  281.   int  changed = 0;
  282.   static long  nexttime, net_time, rc_time;
  283.   struct stat statbuf;
  284.  
  285.   if (Debug || nexttime > secclock()) return;
  286.   nexttime = secclock() + 600;
  287.  
  288.   if (stat("/tcp/net", &statbuf)) return;
  289.   if (!net_time) net_time = statbuf.st_mtime;
  290.   if (net_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  291.     changed = 1;
  292.  
  293.   if (stat(Startup, &statbuf)) return;
  294.   if (!rc_time) rc_time = statbuf.st_mtime;
  295.   if (rc_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  296.     changed = 1;
  297.  
  298.   if (changed) doexit(0, (char **) 0, (void *) 0);
  299. }
  300.  
  301. /*---------------------------------------------------------------------------*/
  302.  
  303. void eihalt()
  304. {
  305.  
  306.   int  n;
  307.   int  status;
  308.   int32 nte;
  309.   register unsigned int  i;
  310.   struct timeval timeout;
  311.  
  312.   check_files_changed();
  313.   wait3(&status, WNOHANG, (int *) 0);
  314.   if (!Debug) alarm(TIMEOUT);
  315.   if (nfds < 0) {
  316.     if (i = chkread[1] | chkwrite[1] | chkexcp[1])
  317.       nfds = 32;
  318.     else {
  319.       i = chkread[0] | chkwrite[0] | chkexcp[0];
  320.       nfds = 0;
  321.     }
  322.     for (n = 16; n; n >>= 1)
  323.       if (i & ((-1) << n)) {
  324.     nfds += n;
  325.     i >>= n;
  326.       }
  327.     if (i) nfds++;
  328.   }
  329.   actread [0] = chkread [0];
  330.   actread [1] = chkread [1];
  331.   actwrite[0] = chkwrite[0];
  332.   actwrite[1] = chkwrite[1];
  333.   actexcp [0] = chkexcp [0];
  334.   actexcp [1] = chkexcp [1];
  335.   timeout.tv_sec = 0;
  336.   if (Hopper)
  337.     timeout.tv_usec = 0;
  338.   else {
  339.     nte = next_timer_event();
  340.     if (nte > 999) nte = 999;
  341.     timeout.tv_usec = 1000 * nte;
  342.   }
  343.   if (select(nfds, actread, actwrite, actexcp, &timeout) < 1) {
  344.     actread [0] = actread [1] = 0;
  345.     actwrite[0] = actwrite[1] = 0;
  346.     actexcp [0] = actexcp [1] = 0;
  347.   } else
  348.     for (n = nfds - 1; n >= 0; n--) {
  349.       if (readfnc [n] && maskset(actread , n)) (*readfnc [n])(readarg [n]);
  350.       if (writefnc[n] && maskset(actwrite, n)) (*writefnc[n])(writearg[n]);
  351.       if (excpfnc [n] && maskset(actexcp , n)) (*excpfnc [n])(excparg [n]);
  352.     }
  353. }
  354.  
  355.